<?php


namespace app\service\impl;


use app\service\PanelService;
use fast\Http;
use think\facade\Log;

class PanelHIKServiceImpl implements PanelService{
    var $_config;

    public function __construct(){
        $this->_config=config('capture.panel');
    }

    public function add_group($group_name,$ape_id,$third_code){
        $data['groupName']=$group_name;
        Log::info('req:'.json_encode($data));
        Log::info('url'.$this->_config['url'].'accessControl/permissionGroups/create');
        $res=Http::post($this->_config['url'].'accessControl/permissionGroups/create',json_encode($data),$this->getToken());
        Log::info('resp:'.$res);
        $res=json_decode($res,true);
        if($res&&$res['code']=200){
            $group_id=$res['data']['groupId'];
            $this->bind_group($group_id,$ape_id);
            return $group_id;
        }
    }

    public function add_people($id, $name, $card_num, $photo, $right){
        $data['employeeNo']=$id;
        $data['personName']=$name;
        $data['cardNo']=$card_num;
        $data['faceImageBase64']=$photo;
        //Log::info('图片：'.$photo);
        Log::info('req:'.json_encode($data));
        //Log::info('url'.$this->_config['url'].'basic/persons/create');
        $res=Http::post($this->_config['url'].'basic/persons/create',json_encode($data),$this->getToken());
        Log::info('resp:'.$res);
        $res=json_decode($res,true);
        if($res&&$res['code']==200){
            $reg['groupId']=$right;
            $reg['employeeNos']=[$id];
            $reg['autoIssue']=true;
            Log::info('req:'.json_encode($reg));
            $res1=Http::post($this->_config['url'].'accessControl/permissionGroups/actions/addPersons',json_encode($reg),$this->getToken());
            Log::info('resp1:'.$res1);
            //新增卡号
            $card['cardNo']=$card_num;
            $card['cardType']='normalCard';
            $card['employeeNo']=$id;
            $cards['cards'][]=$card;
            Log::info('新增卡号req:'.json_encode($cards));
            $card_res=Http::post($this->_config['url'].'basic/cards/batchCreate',json_encode($cards),$this->getToken());
            Log::info('新增卡号resp:'.$card_res);
        }
    }

    private function getToken() {
        return [CURLOPT_HTTPHEADER=>['Content-Type:application/json','Authorization:bearer '.$this->_config['access_token']]];
    }

    public function bind_group($op_rights, $ape_id) {
        $data['groupId']=$op_rights;
        $data['checkCapability']=true;
        $data['deviceSerials']=[$ape_id];
        $data['autoIssue']=true;
        Log::info('绑定权限req:'.json_encode($data));
        $res=Http::post($this->_config['url'].'accessControl/permissionGroups/actions/addDevices',json_encode($data),$this->getToken());
        Log::info('绑定权限resp:'.$res);
    }
}